package com.meishij.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.jash.myutils.utils.ViewUtils;
import com.meishij.R;
import com.meishij.entry.FindWenZhangEntry;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 阿飞 on 2016/10/14.
 */

public class FindWenZhangRecyclerAdapter extends RecyclerView.Adapter<FindWenZhangRecyclerAdapter.ViewHolder> {

    private Context context;
    private LayoutInflater inflater;
    private List<FindWenZhangEntry.ListBean> list;

    public FindWenZhangRecyclerAdapter(Context context, List<FindWenZhangEntry.ListBean> list) {
        this.context = context;
        this.list = list;
        this.inflater = LayoutInflater.from(context);
        if (list == null) {
            this.list = new ArrayList<>();
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflateView = inflater.inflate(R.layout.fragment_find_detail_wenzhang_recycler_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(inflateView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.initView(holder.rootView);

        FindWenZhangEntry.ListBean item = list.get(position);
        String titlepic = item.getTitlepic();
        if (!TextUtils.isEmpty(titlepic)) {
            Uri titlepicUri = Uri.parse(titlepic);
            holder.iv_fragment_find_detail_wenzhang_recycler_titlepic.setImageURI(titlepicUri);
        }

        ViewUtils.setTextView(holder.tv_fragment_find_detail_wenzhang_recycler_title, item.getTitle());
        ViewUtils.setTextView(holder.tv_fragment_find_detail_wenzhang_recycler_smalltext, item.getSmalltext());
    }

    public void add(FindWenZhangEntry.ListBean item) {
        this.list.add(item);
        notifyDataSetChanged();
    }

    public void addAll(List<FindWenZhangEntry.ListBean> items) {
        this.list.addAll(items);
        notifyDataSetChanged();
    }

    public void clear() {
        this.list.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        View rootView;

        SimpleDraweeView iv_fragment_find_detail_wenzhang_recycler_titlepic;
        TextView tv_fragment_find_detail_wenzhang_recycler_title;
        TextView tv_fragment_find_detail_wenzhang_recycler_smalltext;

        public ViewHolder(View itemView) {
            super(itemView);
            rootView = itemView;
        }

        public void initView(View rootView) {
            iv_fragment_find_detail_wenzhang_recycler_titlepic = (SimpleDraweeView) rootView.findViewById(R.id.iv_fragment_find_detail_wenzhang_recycler_titlepic);
            tv_fragment_find_detail_wenzhang_recycler_title = (TextView) rootView.findViewById(R.id.tv_fragment_find_detail_wenzhang_recycler_title);
            tv_fragment_find_detail_wenzhang_recycler_smalltext = (TextView) rootView.findViewById(R.id.tv_fragment_find_detail_wenzhang_recycler_smalltext);

        }
    }
}
